<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <div class="left">
            <span>原理图与源代码</span>
            <img src="../img/3.png" alt="">
            <div class="code">
                <pre class="precode">
public class Subject {


​    //观察者数组
​    private List<Observer> vector = new LinkedList<>();


​    //增加一个观察者
​    public void addObserver(Observer observer) {
​        vector.add(observer);
​    }

​    //删除一个观察者
​    public void deleteObserver(Observer observer) {
​        vector.remove(observer);
​    }

​    //通知所有观察者
​    public void notifyAllObserver() {
​        for(Observer observer : vector) {
​            observer.toDo();
​        }
​    }

​    // 通知特定观察者
​    public void notifyOneObserver(int i) {
​        vector.get(i).toDo();
​    }

}   

public interface Observer {
    void toDo();
}
public class Input implements Observer{

​    private ArrayList<String> lineTxt = new ArrayList<String>();

​    public ArrayList<String> getLineTxt() {
​        return lineTxt;
​    }
​    private String fileName;

​    public Input(String fileName) {
​        this.fileName = fileName;
​    }

​    @Override
​    public void toDo() {
​        BufferedReader inputFile = null;
​        try {
​            inputFile = new BufferedReader(new FileReader(fileName));
​        } catch (Exception e) {
​            e.printStackTrace();
​        }
​        String line;
​        try {
​            while ((line = inputFile.readLine()) != null) {
​                lineTxt.add(line);
​            }
​        } catch (IOException e) {
​            e.printStackTrace();
​        }
​    }
}

public class Shift implements Observer {
    private ArrayList<String> kwicList = new ArrayList<String>();
    private ArrayList<String> lineTxt;

​    public Shift( ArrayList<String> lineTxt) {
​        this.lineTxt = lineTxt;
​    }

​    public ArrayList<String> getKwicList() {
​        return kwicList;
​    }


​    public ArrayList<String> getLineTxt() {
​        return lineTxt;
​    }

​    @Override
​    public void toDo() {
​        //获取每个单词，存入tokens
​        Iterator<String> it = lineTxt.iterator();
​        while (it.hasNext()) {
​            StringTokenizer token = new StringTokenizer(it.next());
​            ArrayList<String> tokens = new ArrayList<String>();
​            int i = 0;
​            //循环添加单词
​            int count = token.countTokens();
​            while (i < count) {
​                tokens.add(token.nextToken());
​                i++;
​            }

​            //display(tokens);
​            //切割各个单词，不断改变起始值和利用loop实现位移。
​            for (i = 0; i < count; i++) {
​                StringBuffer lineBuffer = new StringBuffer();
​                int index = i;
​                for (int f = 0; f < count; f++) {
​                    //从头继续位移
​                    if (index >= count)
​                        index = 0;
​                    //存入StringBuffer
​                    lineBuffer.append(tokens.get(index));
​                    lineBuffer.append(" ");
​                    index++;
​                }
​                String tmp = lineBuffer.toString();
​                kwicList.add(tmp);
​            }
​        }

​    }

}
public class Alphabetizer implements Observer {
    private ArrayList<String> kwicList;

​    public Alphabetizer(ArrayList<String> kwicList) {
​        this.kwicList = kwicList;
​    }

​    public ArrayList<String> getKwicList() {
​        return kwicList;
​    }

​    @Override
​    public void toDo() {
​        Collections.sort(this.kwicList, new AlphabetizerComparator());
​    }

​    private class AlphabetizerComparator implements Comparator<String> {
​        @Override
​        public int compare(String o1, String o2) {
​            if (o1 == null && o2 == null) {
​                throw new NullPointerException();
​            }
​            int compareValue = 0;
​            char o1c = o1.toLowerCase().charAt(0); //忽略大小写
​            char o2c = o2.toLowerCase().charAt(0); //忽略大小写
​            compareValue = o1c - o2c;
​            return compareValue;

​        }

​    }

}
public class Output implements Observer {
    private ArrayList<String> kwicList;
    private BufferedWriter outputFile;
    private String filename;
    public Output(ArrayList<String> kwicList,String filename) {
        this.kwicList = kwicList;
        this.filename = filename;
    }

​    @Override
​    public void toDo(){
​        Iterator<String> it = kwicList.iterator();
​        try {
​            outputFile = new BufferedWriter(new FileWriter(filename));
​            while (it.hasNext()) {
​                outputFile.write(it.next()+"\n");
​            }
​        }catch (IOException e){
​            e.printStackTrace();
​        }finally {
​            try {
​                if (outputFile!=null) {
​                    outputFile.close();
​                }
​            } catch (IOException e) {
​                e.printStackTrace();
​            }
​        }
​    }

}
public class Main {
    public static void main(String[] args) {
        //创建主题
        KWICSubject kwicSubject = new KWICSubject();
        //创建观察者
        Input input = new Input("E:\\input.txt");
        Shift shift = new Shift(input.getLineTxt());
        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());
        Output output = new Output(alphabetizer.getKwicList(), "E:\\output.txt");

​        // 将观察者加入主题
​        kwicSubject.addObserver(input);
​        kwicSubject.addObserver(shift);
​        kwicSubject.addObserver(alphabetizer);
​        kwicSubject.addObserver(output);
​        // 逐步调用各个观察者
​        kwicSubject.startKWIC();
​    }
}

</pre>
            </div>
        </div>

        <div class="right">
            <span>文件上传</span>
    <input type="file" id="inputFile" accept=".txt">
    <button onclick="processFile()">Process File</button>
            <div class="original">
                <span>你的文本</span>
                <div id="fileContent"></div></div>
                <span>修改过后</span>
            <div class="changed"><pre id="output"></pre></div>
        </div>

​    </div>
</body>
<style>
    .container {
        display: flex;
        justify-content:space-around;
    }
    .right,.left {
        width: 600px;
        display: flex;
        flex-direction: column;
        align-items: center;
        border: 1px solid rgb(134, 134, 138);
        border-radius: 5px;
    }
   .left>span {
        font-size: 20px;
        font-weight: bolder;
    }
    .code {
        width: 500px;
    }
    .precode {
        background-color: #dcddd9;
    }
.original span{
    font-size: 20px;
}
</style>
<script>
    // 获取文件输入元素
    var input = document.getElementById('inputFile');
    // 获取用于显示文件内容的元素
    var fileContent = document.getElementById('fileContent');

    // 为文件输入元素添加'change'事件监听器
    input.addEventListener('change', function(e) {
        var files = e.target.files; // 获取文件列表
        if (files.length) {
            var file = files[0]; // 获取单个文件
            var reader = new FileReader(); // 创建FileReader对象

            // 为FileReader对象添加'load'事件监听器
            reader.onload = function(e) {
                // e.target.result 包含了文件的内容
                fileContent.textContent = e.target.result;
            };

            // 读取文件内容
            reader.readAsText(file);
        }
    });
     function processFile() {
    const input = document.getElementById('inputFile').files[0];
    if (input) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const text = e.target.result;
            const kwicProcessor = new KWICProcessor();
            kwicProcessor.input(text);
            kwicProcessor.shift();
            kwicProcessor.alphabetizer();
            document.getElementById('output').textContent = kwicProcessor.output();
        };
        reader.readAsText(input);
    }
}

class KWICProcessor {
    constructor() {
        this.kwicList = [];
        this.lineTxt = [];
    }

    input(text) {
        this.lineTxt = text.split('\n');
    }

    shift() {
        this.lineTxt.forEach(line => {
            const tokens = line.split(/\s+/);
            for (let i = 0; i < tokens.length; i++) {
                let lineBuffer = [];
                for (let f = 0; f < tokens.length; f++) {
                    let index = (i + f) % tokens.length;
                    lineBuffer.push(tokens[index]);
                    if (f < tokens.length - 1) lineBuffer.push(" ");
                }
                this.kwicList.push(lineBuffer.join(""));
            }
        });
    }

    alphabetizer() {
        this.kwicList.sort((a, b) => {
            const lowerA = a.toLowerCase();
            const lowerB = b.toLowerCase();
            return lowerA.localeCompare(lowerB);
        });
    }

    output() {
        return this.kwicList.join('\n');
    }
}
</script>
</html>